<?php

/**
 * Created by PhpStorm.
 * User: SunnyLau
 * Date: 2017/3/17
 * Time: 16:21
 */
class cache
{
	public static function set($key, $value, $expire = 0) {
		$record = fpsDB::findOne(APP_TABLE_CACHE, ["key"=> $key]);
		if(!$record)
		{
			$record = ["key"=> $key, "_id"=> fpsDB::index('cache')];
		}

		$record["value"] = $value;
		$record["expire"] = $expire ? ($expire + time()) : 0;
		fpsDB::save(APP_TABLE_CACHE, $record);
	}

	public static function get($key) {
		$record = fpsDB::findOne(APP_TABLE_CACHE, ["key"=> $key]);
		if($record && (!$record["expire"] || $record["expire"] > time()))
		{
			return $record["value"];
		}
		else
		{
			return false;
		}
	}

}